package com.jy.modules.cims.component.balance;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.sysUtils.CollectionsUtils;
import com.jy.modules.cims.common.sysUtils.SearchParamUtil;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.data.common.dao.TCMmCostAffiliationDao;
import com.jy.modules.cims.data.common.dto.TCMmCostAffiliationDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDealDetailDTO;
import com.jy.modules.cims.data.dao.CreditDealDetailDao;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.data.dto.credit.CostAffiliationResultDto;

@Service("com.jy.modules.cims.component.balance.SettleUtilsService")
public class SettleUtilsService {

	

	@Autowired
	private CreditDealDetailDao creditDealDetailDao;
	

	@Autowired
	private TCMmCostAffiliationDao tcmmCostAffiliationDao;
	/**
	 * 
	 * @Description 生成结算编号
	 * @param dealCode
	 * @return   
	 * String  
	 * @throws
	 * @author zhangmi
	 * @date 2016-4-18
	 */
	public String autoMakeSettleApplyCode(String dealCode){
		TCMmCreditDealDetailDTO creditDealDetail = creditDealDetailDao.getMaxSettleApplyId(dealCode);
		String settleApplyId = creditDealDetail.getSettleApplyId().trim();
		if("0".equals(settleApplyId)||!SimpleStringUtils.hasText(settleApplyId)){
			return dealCode+"-01";
		}else{
			String tempEnd = settleApplyId.split("-")[1];
			String newEnd = (Integer.parseInt(tempEnd)+1)+"";
			if(newEnd.length()<2){
				return dealCode+"-0"+newEnd;
			}else{
				return dealCode+"-"+newEnd;
			}
		}
	}
	
	

	/**
	 * 
	 * @Description 归属判断
	 * @param dto
	 * @return   
	 * costAffiliationResultDto  
	 * @throws
	 * @author zhangmi
	 * @date 2016-4-25
	 */
	public CostAffiliationResultDto checkCost(TCMmCostAffiliationDTO dto){
		List<TCMmCostAffiliationDTO> list = tcmmCostAffiliationDao.searchTCMmCostAffiliation(SearchParamUtil.getSearchParam(dto));
		CostAffiliationResultDto result = new CostAffiliationResultDto();
		if(CollectionsUtils.isNotEmpty(list)){
			TCMmCostAffiliationDTO costAffiliationDto = list.get(0);
			//服务费
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getServiceFeeAff()))
				result.setServiceFee(true);
			//担保费
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getGuaranteeFeeAff()))
				result.setGuaranteeFee(true);
			//保证金
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getMarginAff()))
				result.setMarginFee(true);
		}
		return result;
	}
	
	public CostAffiliationResultDto checkCostByDealCode(String dealCode,String businessType){
		DealDto dto = new DealDto();
		dto.setDealCode(dealCode);
		dto.setDealType(businessType);
		List<TCMmCostAffiliationDTO> list = creditDealDetailDao.getCostAffiliationByDealCode(SearchParamUtil.getSearchParam(dto));
		CostAffiliationResultDto result = new CostAffiliationResultDto();
		if(CollectionsUtils.isNotEmpty(list)){
			TCMmCostAffiliationDTO costAffiliationDto = list.get(0);
			//服务费
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getServiceFeeAff()))
				result.setServiceFee(true);
			//担保费
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getGuaranteeFeeAff()))
				result.setGuaranteeFee(true);
			//保证金
			if(CimsConstant.FEE_AFF_JS.equals(costAffiliationDto.getMarginAff()))
				result.setMarginFee(true);
			
		}
		return result;
	}
}
